package com.sikaryofficial.workbench.manager;

import cn.hutool.core.collection.CollUtil;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.RemoteNoticeService;
import com.sikaryofficial.system.api.model.notice.NoticePageReq;
import com.sikaryofficial.system.api.model.notice.NoticeResp;
import com.sikaryofficial.workbench.domain.dto.resp.notify.UserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class NoticeManager {

    @Autowired
    private RemoteNoticeService remoteNoticeService;

    public List<UserNotice> listNotice(Long noticeId, Integer size) {
        NoticePageReq req = new NoticePageReq();
        req.setNoticeId(noticeId);
        req.setUserId(SecurityUtils.getUserId());
        req.setSize(size);
        R<List<NoticeResp>> noticeRespPageResult = remoteNoticeService.listUserNotice(req);
        if (Boolean.TRUE.equals(R.isError(noticeRespPageResult))) {
            return new ArrayList<>();
        }
        List<NoticeResp> data = noticeRespPageResult.getData();
        if (CollUtil.isEmpty(data)) {
            return new ArrayList<>();
        }
        return data.stream().map(item -> {
                    UserNotice userNotice = new UserNotice();
                    userNotice.setNoticeId(item.getNoticeId());
                    userNotice.setNoticeTitle(item.getNoticeTitle());
                    userNotice.setNoticeContent(item.getNoticeContent());
                    userNotice.setCreateTime(item.getCreateTime());
                    return userNotice;
                })
                .collect(Collectors.toList());
    }

}
